package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_promote;
import cn.com.api.entity.ApiTmember;
import cn.com.api.service.ApiT_promoteService;
import cn.com.api.service.StartOnLoadService;
import cn.com.util.*;
import cn.com.util.*;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE+"/")
public class T_promoteController{

	private static Logger logger = Logger.getLogger(T_promoteController.class);
	@Autowired
	private StartOnLoadService startOnLoadService;
	@Autowired
	private ApiT_promoteService apiT_promoteService;
	
	@RequestMapping(value = "t_promote_list")
	public ModelAndView t_promote_list(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_promote_list.jsp");
		String city_id = request.getParameter("city_id");
		Map<String,Object> conditions = new HashMap<String,Object>();
		conditions.put("city_id", city_id);
		
		List<ApiT_promote> promoteList = apiT_promoteService.searchPromoteList(conditions, pageInfo);
		mv.addObject("list", promoteList);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("conditions", conditions);
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		return mv;
	} 
	
	@RequestMapping(value = "t_promote_app_list")
	public ModelAndView t_promote_app_list(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_promote_app_list.jsp");
		String city_id = request.getParameter("city_id");//分站
		String start_time = request.getParameter("start_time");//开始时间
		String end_time = request.getParameter("end_time");//结束时间
		String name = request.getParameter("name");//推广人姓名
		String invite_code = request.getParameter("invite_code");//邀请码
		Map<String,Object> conditions = new HashMap<String,Object>();
		conditions.put("city_id", city_id);
		conditions.put("start_time", start_time);
		conditions.put("end_time", end_time);
		conditions.put("name", name);
		conditions.put("invite_code", invite_code);
		
		
		List<ApiT_promote> promoteAppList = apiT_promoteService.searchPromoteAppList(conditions, pageInfo);
		mv.addObject("list", promoteAppList);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("conditions", conditions);
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		return mv;
	}
	
	@RequestMapping(value = "t_promote_app_detail_list")
	public ModelAndView t_promote_app_detail_list(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_promote_app_detail_list.jsp");
		String start_time = request.getParameter("start_time");//开始时间
		String end_time = request.getParameter("end_time");//结束时间
		String id = request.getParameter("id");
		Map<String,Object> conditions = new HashMap<String,Object>();
		conditions.put("start_time", start_time);
		conditions.put("end_time", end_time);
		conditions.put("id", id);
		
		List<ApiTmember> promoteAppList = apiT_promoteService.searchPromoteAppDetailList(conditions, pageInfo);
		mv.addObject("list", promoteAppList);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("conditions", conditions);
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		return mv;
	}
	
	@RequestMapping(value = "t_promote_add_pre")
	public ModelAndView t_promote_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_promote_add.jsp");
		
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		return mv;
	} 
	
	@RequestMapping(value = "t_promote_addOrUpdate_pre")
	public ModelAndView t_promote_addOrUpdate_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_promote_add.jsp");
		
		String id = request.getParameter("id");
		ApiT_promote t_promote = apiT_promoteService.searchPromoteById(id);
		
		
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		mv.addObject("t_promote", t_promote);
		return mv;
	} 
	
	@RequestMapping(value = "t_promote_addOrUpdate")
	public void t_promote_addOrUpdate(HttpServletRequest request,
			HttpServletResponse response,ApiT_promote t_promote) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			//校验邀请码的唯一性
			if(apiT_promoteService.checkInviteCode(t_promote)){
				apiT_promoteService.addOrUpdatePromote(t_promote);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "该邀请码已被使用！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		System.out.println(JsonUtil.object2json(map));
		out.close();
		
	} 
	
	@RequestMapping(value = "t_promote_delete")
	public void t_promote_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			String id = request.getParameter("id");
			apiT_promoteService.deletePromoteById(id);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}catch(Exception e){
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
		
	} 
	
}



